// ----------------------------------------------------------------------------
// byte_reader.cpp
// ----------------------------------------------------------------------------
#include <fw/base/serialize/byte_reader.h>
#include <fw/base/core/assertion.h>

namespace fw
{
	ByteReader::ByteReader(const char* data, size_t size)
	{
		SetBuffer(data, size);
	}
	
	ByteReader::ByteReader()
	{
		SetBuffer(nullptr, 0);
	}
	
	void ByteReader::SetBuffer(const char* data, size_t size)
	{
		m_data   = data;
		m_size   = size;
		m_offset = 0;
	}
	
	const char* ByteReader::GetBuffer() const
	{
		return m_data;
	}
	
	void ByteReader::Seek(size_t pos)
	{
		m_offset = pos;
	}
	
	size_t ByteReader::Tell() const
	{
		return m_offset;
	}
	
	uint8_t  ByteReader::Read8()
	{
		FW_ASSERT(m_offset<m_size);
		return m_data[m_offset++];
	}
	
	uint16_t ByteReader::Read16()
	{
		uint16_t hi = static_cast<uint16_t>(Read8());
		uint16_t lo = static_cast<uint16_t>(Read8());
		return (hi << 8) | lo;
	}
	
	uint32_t ByteReader::Read32()
	{
		uint32_t hi = static_cast<uint32_t>(Read16());
		uint32_t lo = static_cast<uint32_t>(Read16());
		return (hi << 16) | lo;
	}

	uint64_t ByteReader::Read64()
	{
		uint64_t hi = static_cast<uint64_t>(Read32());
		uint64_t lo = static_cast<uint64_t>(Read32());
		return (hi << 32) | lo;
	}
	
	size_t ByteReader::GetRemain() const
	{
		return m_size - m_offset;
	}
	
} // namespace fw
